#!/bin/bash
# %Z%%M%        %I%  %W% %G% %U%

export PATH=$PATH:/opt/IBMJava/jre/bin

#
# If the DEBUG_FILE is not empty, then
# source the file so that environment
# variables are set.
# 
DEBUG_FILE=/var/hsc/log/.DEBUG_HSC
[ -s ${DEBUG_FILE} ] &&
{
. ${DEBUG_FILE} 
}

trap "/opt/hsc/bin/initCIMOM stop" 2 6 7 11 15
export JAVA_COMPILER=NONE
x=`type -p java 2>/dev/null`
if [ "$x" != "" ]
then
  JAVAPATH=`/usr/bin/dirname $x`
fi
export LD_LIBRARY_PATH=/opt/hsc/lib/:/usr/lib/:$LD_LIBRARY_PATH
if [ "${DEBUG_JARS_DIRECTORY}" != "" ] ; then
  if [ -d ${DEBUG_JARS_DIRECTORY} ] ; then
    for i in ${DEBUG_JARS_DIRECTORY}/*.jar
    do
       debug_jars=${debug_jars}:$i
    done
  fi
fi

CLASSPATH=${DEBUG_JARS_DIRECTORY}:${debug_jars}:/usr/websm/codebase/pluginjars/ccfw.jar:/usr/websm/codebase/pluginjars/ccfw_sslite.jar:/usr/websm/codebase/pluginjars/auifw.jar:/usr/sbin/rsct/codebase:/usr/websm/codebase/pluginjars/sniacimom.jar:/usr/websm/codebase/pluginjars/xerces.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/hsc.jar:/usr/websm/codebase/pluginjars/aca.jar:$CLASSPATH
export CLASSPATH
PATH=$JAVAPATH:/opt/hsc/bin:$PATH
export PATH
CIMOMID=/var/hsc/sniacimom.pid
TRUNCATE_CIMLOG=/tmp/.truncate_cimlog
HEAP_MEM=180
MEM=`(/usr/bin/free -mo|/bin/awk '{ if (NR == 2) print $2}') 2> /dev/null`
if  [ ! -z $MEM ] ; then
    (( MEM /= 2))	
    HEAP_MEM=$MEM
	if [ $HEAP_MEM -gt 512 ]; then
		HEAP_MEM=512
	fi	
fi
# Set stack size to 2M to work around Java problem
ulimit -s 2048
currDir=`pwd`
case $1 in
start )
 # QW 709. Clean the CIMOM repository first.
    echo CLEANING Repository ...
    if [ -d /var/sniacimom/persistence/instances/root/ibmhscV3_2 ]
    then
       echo delete /var/sniacimom/persistence/instances/root/ibmhscV3_2
       rm -rf /var/sniacimom/persistence/instances/root/ibmhscV3_2
    fi
    if [ -d /var/sniacimom/persistence/instances/root/ibmhscS1_0 ]
    then
       echo delete /var/sniacimom/persistence/instances/root/ibmhscS1_0
       rm -rf /var/sniacimom/persistence/instances/root/ibmhscS1_0
    fi

    cd /opt/hsc/bin


    if [ -f ${DEBUG_FILE} ]; then
       java ${JPDA_DEBUG} -Xoss512k -Xms20m -Xmx${HEAP_MEM}m -Djava.compiler=NONE -Djavax.net.ssl.keyStore=/var/websm/security/SM.privkr -Djavax.net.ssl.keyStorePassword=defp -Djava.security.auth.login.config=/opt/hsc/data/cimom.config -Djava.security.auth.policy=/opt/hsc/data/hscAccess.policy -Dorg.snia.wbem.cimom.properties=/opt/hsc/data/cim.properties -Dcim.server.log=/var/hsc/log/cimserver.log com.ibm.hsc.common.util.CIMServerLoader &
       pid=$!
    else
       java ${JPDA_DEBUG} -Xoss512k -Xms20m -Xmx${HEAP_MEM}m -Djavax.net.ssl.keyStore=/var/websm/security/SM.privkr -Djavax.net.ssl.keyStorePassword=defp -Djava.security.auth.login.config=/opt/hsc/data/cimom.config -Djava.security.auth.policy=/opt/hsc/data/hscAccess.policy -Dorg.snia.wbem.cimom.properties=/opt/hsc/data/cim.properties com.ibm.hsc.common.util.CIMServerLoader &
       pid=$!
    fi

    if [ ! -d /var/hsc ]
    then
       mkdir /var/hsc
    fi
    echo $pid > $CIMOMID
    /bin/chmod 0440 $CIMOMID
    echo "Cimom (PID=$pid) started"

 # QW 709. Start HscConsole here. initCIMOM will waiting the CIMOM stop.
    java -Xms20m -Xmx180m -Djavax.net.ssl.keyStore=/usr/websm/codebase/SM.pubkr -Djavax.net.ssl.keyStorePassword=defp -Dorg.snia.wbem.cimom.properties=/opt/hsc/data/cim.properties com.ibm.hsc.console.CreateHscConsole &

 # restart RMC
    STOP_PID=`ps -efwww | grep refrsrc | grep -v grep | cut -c10-15`
    if [ "$STOP_PID" != "" ]
    then
       echo Stopping refrsrc
       kill -9 $STOP_PID >/dev/null 2>&1
    fi

    if [ -f /usr/sbin/rsct/bin/refrsrc ]
    then
   export CT_MANAGEMENT_SCOPE=1
   /usr/sbin/rsct/bin/refrsrc IBM.NodeAuthenticate &
    fi
    wait $pid
    echo "Cimom stopped on "`date` >/tmp/cimserverStopped.log

    cd $currDir
    ;;

stop )
   if [ -f $CIMOMID ]; then
      touch $TRUNCATE_CIMLOG
      pid=`cat $CIMOMID`
   # Here the pid is the CIMServer's pid.
      kill -15 $pid >/dev/null 2>&1
      rm -f $CIMOMID
      echo "Cimom (PID=$pid) stopped"
      echo "Cimom (PID=$pid) stopped on `date` > /tmp/cimserverStopped.log"
   else
      echo "Could not find any running CIMOM"
   fi
   STOP_PID=`ps -efwww | grep CIMServer | grep -v grep | cut -c10-15`
   if [ "$STOP_PID" != "" ]
   then
       echo Stopping CIMServer
       kill -9 $STOP_PID >/dev/null 2>&1
   fi

   # Do not use kill to stop this initCIMOM, it may results in the loop.
   STOP_PID=`ps -efwww | grep initCIMOM | grep -v grep | cut -c10-15`
   echo "The initCIMOM ($STOP_PID) is still there >> /tmp/cimserverStopped.log"

   # Stop /usr/sbin/rsct/bin/refrsrc
   STOP_PID=`ps -efwww | grep refrsrc | grep -v grep | cut -c10-15`
   if [ "$STOP_PID" != "" ]
   then
       echo Stopping /usr/sbin/rsct/bin/refrsrc
       kill -9 $STOP_PID >/dev/null 2>&1
   fi

   ;;
esac
